package linter.linterjava.utils;

import com.google.gson.Gson;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import static linter.linterjava.custom.Constant.*;
import static linter.linterjava.custom.CustomResultType.UPLOAD_ERROR;
import static linter.linterjava.utils.ModelUtils.generateUUID;

/**
 * @author 北煜
 */
public class UploadFileUtil {


    /**
     * 这是七牛云上传文件的工具类
     *
     * @param file 上传的图像文件
     * @param uuid 用户id
     * @param type 上传到何处
     * @return 返回图像的url
     */
    public static String uploadImageToQiniu(MultipartFile file, Long uuid, String type){
        Configuration cfg = new Configuration(Region.region2());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(ACCESS_KEY_QINIU, SECRET_KEY_QINIU);
        String bucketName = BUCKET_NAME_QINIU;
        String key = "User/" + type + "/images/" + uuid + "/" + generateUUID() + file.getOriginalFilename();
        String baseUrl = "http://qiniuyun.linter.top";
        String url = baseUrl + "/" + key;

        try {
            byte[] fileContent = file.getBytes();
            try (ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContent)) {
                String upToken = auth.uploadToken(bucketName);
                Response response = uploadManager.put(inputStream, key, upToken, null, null);
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

                if (response.statusCode == RES_SUCCESS) {
                    return url;
                } else {
                    throw new RuntimeException(UPLOAD_ERROR + ": " + response.bodyString());
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param file 上传的数据集
     * @param uuid 用户id
     * @param type 使用的模型
     * @return 返回数据集URL
     */
    public static String uploadFileToQiniu(MultipartFile file, Long uuid, String type){
        Configuration cfg = new Configuration(Region.region2());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(ACCESS_KEY_QINIU, SECRET_KEY_QINIU);
        String bucketName = BUCKET_NAME_QINIU;
        String key = "User/" + type + "/Model/" + uuid + "/" + file.getOriginalFilename();
        String baseUrl = "http://qiniuyun.linter.top";
        String url = baseUrl + "/" + key;

        try {
            byte[] fileContent = file.getBytes();
            try (ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContent)) {
                String upToken = auth.uploadToken(bucketName);
                Response response = uploadManager.put(inputStream, key, upToken, null, null);
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

                if (response.statusCode == RES_SUCCESS) {
                    return url;
                } else {
                    throw new RuntimeException(UPLOAD_ERROR + ": " + response.bodyString());
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
